<?php
/**
 * <i>@include</i>组件
 * 
 * 包含一个文件或模板
 * 
 * 可以使用的参数有：
 * - [x-file] 	string 文件名
 * - [x-template] string  模板名
 * - [x-format]   string 文档格式:xml,html
 *
 * @version $Id: IMintIncludeComponent.php 312 2008-04-23 16:29:38Z iwind $
 * @package util.mint.component
 */
class IMintIncludeComponent extends IAbstractMintComponent {
	/**
	 * 取得支持的参数列表
	 *
	 * @return array
	 */	
	public function getSupportedParameters() {
		return array("x-file", "x-template", "x-format");
	}
	
	/**
	 * 取得HTML属性映射
	 *
	 * @return array
	 * @since 0.0.3
	 */		
	public function getHtmlAttrMapping() {
		return array();
	}	
	
	
	/**
	 * 处理组件
	 *
	 * @return boolean
	 */	
	public function processComponent() {
		$parameters = $this->getParameters();
		
		if (isset($parameters["x-file"])) {
			$path = $parameters["x-file"];
			if (IFile::isAbsolutePath($path)) {
				$file = new IFile($path);
			}
			else {
				$file = new IFile($this->getTemplate()->getTemplateFile()->getParent() . "/" . $path);				
			}
			if ($file->isFile() && $file->exists()) {
				$this->getTemplate()->addObservedFile($file);
				$this->addLastChildText($file->getReader()->read());;
				return true;
			}
			else {
				throw new IMintException("can't find file '{$path}' declared in parameter in component " . $this->getName());
			}
		}
		else if (isset($parameters["x-template"])) {
			$templateName = $parameters["x-template"];			
			$format = IMintConfig::HTML_FORMAT;
			if (isset($parameters["x-format"])) {
				if (isset(IMintConfig::$formatTexts[$parameters["x-format"]])) {
					$format = IMintConfig::$formatTexts[$parameters["x-format"]];
				}
				else {
					throw new IMintException("can't parse format '{$parameters['x-format']}' for included template '{$templateName}'");
				}
			}
			$file = IMint::getFile($templateName, $format);
			if ($this->getTemplate()->getTemplateFile()->getAbsolutePath() == $file->getAbsolutePath()) {
				throw new IMintException("occured at " . $this->getNodeDescription() . "<br/>" . $file->getPath() . " is forbidden to contain 'include' component with template parameter's value '{$templateName}'.");
			}
			
			$this->getTemplate()->addObservedFile($file);
			$template = new IMintTemplate($templateName, $format);
			$this->addLastChildScript($template->getCompiledText());
			return true;
		}
		else {
			throw new IMintException("can't find x-file or x-template parameter in component " . $this->getName());
		}
	}
}

?>